import { BaseMsgHandler } from "../../framework/BaseMsgHandler";
import { ClientMsg } from "../../framework/common/ClientMsg";
import { DebugClient } from "../DebugClient";
import { DebugHost } from "../DebugHost";
import { EClientState } from "../../framework/common/EClientState";
import { EMessageC2S } from "../../../common/EMessageC2S";
import { WrapRegistData } from "../../../common/CommonTransData";
import { EClientType } from "../../../common/EClientType";

export class HandleRegist extends BaseMsgHandler<DebugHost> {

    msgId: number = EMessageC2S.Regist;

    HandleMsg(msg: ClientMsg) {
        let client = msg.client as DebugClient;
        if (client.state == EClientState.Wait && client.type == EClientType.Undefine) {
            let parseData = msg.transData.data as WrapRegistData;
            switch (parseData.registType) {
                case EClientType.Client:
                    client.type = EClientType.Client;
                    client.state = EClientState.Idle;
                    client.name = parseData.clientName;
                    client.logger.Log(client.remoteAddress + "注册为client");
                    break;
                case EClientType.Debugger:
                    client.type = EClientType.Debugger;
                    client.state = EClientState.Idle;
                    client.name = parseData.clientName;
                    client.logger.Log(client.remoteAddress + "注册为debugger");
                    break;
                default:
                    client.logger.Log("未处理的消息:" + JSON.stringify(msg.transData));
                    client.DisConnect();
                    break;
            }
            return;
        }
        client.logger.Log('接收到重复1001消息');
    }

}